<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="主板ID" prop="boardNo">
        <el-input
          v-model="queryParams.boardNo"
          placeholder="请输入主板ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主板用途" prop="purpose">
        <el-input
          v-model="queryParams.purpose"
          placeholder="请输入主板用途"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:board:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:board:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:board:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:board:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="boardList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主板ID" align="center" prop="id" />
      <el-table-column label="主板ID" align="center" prop="boardNo" />
      <el-table-column label="主板用途" align="center" prop="purpose" />
      <el-table-column label="绑定站台" align="center" prop="stopId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.stopId"/>
        </template>
      </el-table-column>
      <el-table-column label="截图" align="center" prop="shot" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.shot" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="led截图" align="center" prop="ledShot" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.ledShot" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="是否在线" align="center" prop="online">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_job_status" :value="scope.row.online"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="memo" />
      <el-table-column label="传感器" align="center" prop="sensor" />
      <el-table-column label="定时开关机" align="center" prop="openCron" />
      <el-table-column label="节目名称" align="center" prop="program" />
      <el-table-column label="节目状态" align="center" prop="programStat">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.programStat"/>
        </template>
      </el-table-column>
      <el-table-column label="下载进度" align="center" prop="downRate" />
      <el-table-column label="车辆ID" align="center" prop="carId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.carId"/>
        </template>
      </el-table-column>
      <el-table-column label="设备型号" align="center" prop="equipModel" />
      <el-table-column label="系统版本" align="center" prop="versionCode" />
      <el-table-column label="固件厂商" align="center" prop="facturer" />
      <el-table-column label="是否激活 1是0否" align="center" prop="activate">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.activate"/>
        </template>
      </el-table-column>
      <el-table-column label="样式ID" align="center" prop="stype">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.stype"/>
        </template>
      </el-table-column>
      <el-table-column label="音量计划" align="center" prop="volumePlan" />
      <el-table-column label="程序版本" align="center" prop="version" />
      <el-table-column label="注册时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="编辑时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:board:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:board:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改主板信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="主板ID" prop="boardNo">
          <el-input v-model="form.boardNo" placeholder="请输入主板ID" />
        </el-form-item>
        <el-form-item label="主板用途" prop="purpose">
          <el-input v-model="form.purpose" placeholder="请输入主板用途" />
        </el-form-item>
        <el-form-item label="绑定站台" prop="stopId">
          <el-select v-model="form.stopId" placeholder="请选择绑定站台">
            <el-option
              v-for="item in stopOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="截图" prop="shot">
          <image-upload v-model="form.shot"/>
        </el-form-item>
        <el-form-item label="led截图" prop="ledShot">
          <image-upload v-model="form.ledShot"/>
        </el-form-item>
        <el-form-item label="是否在线 1是 0否" prop="online">
          <el-radio-group v-model="form.online">
            <el-radio
              v-for="dict in dict.type.sys_job_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="memo">
          <el-input v-model="form.memo" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="传感器" prop="sensor">
          <el-input v-model="form.sensor" placeholder="请输入传感器" />
        </el-form-item>
        <el-form-item label="定时开关机" prop="openCron">
          <el-input v-model="form.openCron" placeholder="请输入定时开关机" />
        </el-form-item>
        <el-form-item label="节目名称" prop="program">
          <el-input v-model="form.program" placeholder="请输入节目名称" />
        </el-form-item>
        <el-form-item label="节目状态" prop="programStat">
          <el-radio-group v-model="form.programStat">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="下载进度" prop="downRate">
          <el-input v-model="form.downRate" placeholder="请输入下载进度" />
        </el-form-item>
        <el-form-item label="车辆" prop="carId">
          <el-select v-model="form.carId" placeholder="请选择车辆ID">
            <el-option
              v-for="item in carOptions"
              :key="item.id"
              :label="item.carNo"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备型号" prop="equipModel">
          <el-input v-model="form.equipModel" placeholder="请输入设备型号" />
        </el-form-item>
        <el-form-item label="系统版本" prop="versionCode">
          <el-input v-model="form.versionCode" placeholder="请输入系统版本" />
        </el-form-item>
        <el-form-item label="固件厂商" prop="facturer">
          <el-input v-model="form.facturer" placeholder="请输入固件厂商" />
        </el-form-item>
        <el-form-item label="是否激活 1是0否" prop="activate">
          <el-radio-group v-model="form.activate">
            <el-radio
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="样式ID" prop="stype">
          <el-select v-model="form.stype" placeholder="请选择样式ID">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="音量计划" prop="volumePlan">
          <el-input v-model="form.volumePlan" placeholder="请输入音量计划" />
        </el-form-item>
        <el-form-item label="程序版本" prop="version">
          <el-input v-model="form.version" placeholder="请输入程序版本" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBoard, getBoard, delBoard, addBoard, updateBoard } from "@/api/system/board";

export default {
  name: "Board",
  dicts: ['sys_job_status', 'sys_yes_no', 'sys_user_sex', 'sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 主板信息表格数据
      boardList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      stopOptions: [],
      carOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        boardNo: null,
        purpose: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询主板信息列表 */
    getList() {
      this.loading = true;
      listBoard(this.queryParams).then(response => {
        this.boardList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        boardNo: null,
        purpose: null,
        stopId: null,
        shot: null,
        ledShot: null,
        online: null,
        memo: null,
        sensor: null,
        openCron: null,
        program: null,
        programStat: null,
        downRate: null,
        carId: null,
        equipModel: null,
        versionCode: null,
        facturer: null,
        activate: null,
        stype: null,
        volumePlan: null,
        version: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getBoard(0).then(response => {
              this.stopOptions = response.stops;
              this.carOptions = response.cars;
              this.open = true;
              this.title = "添加主板信息";
            });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBoard(id).then(response => {
        this.form = response.data;
        this.stopOptions = response.stops;
        this.carOptions = response.cars;
        this.open = true;
        this.title = "修改主板信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBoard(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBoard(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除当前数据？').then(function() {
        return delBoard(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/board/export', {
        ...this.queryParams
      }, `board_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
